/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game.platformer.interfaces;
import game.platformer.Player;
import game.platformer.combat.BulletPool;
import game.platformer.levels.Level;
import game.platformer.levels.levelPlatform;
import game.platformer.platformer;
import java.util.ArrayList;
import java.util.LinkedList;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.UpdateHandlerList;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
/**
 *
 * @author cow
 */
public class MapScreen extends Scene {
	//varblock---------------
	platformer game;
	Camera viewport;
	AnalogOnScreenControl cStick;
	AnalogOnScreenControl cStickTwo;
	HUD hud;
	private LinkedList<levelPlatform> removeablePlatforms;
	private LinkedList<IEntity> removeableSprites;
	static final float cstickScale = 1f;
	//-----------------------
	//Textures---------------
	Texture cBaseT, cKnobT;
	TextureRegion cBaseTR, cKnobTR;
	Texture hudItemsT;
	TextureRegion hudMenuBtnTR;
	TextureRegion hudHeartFullTR, hudHeartEmptyTR;
	Texture bulletsT;
	TextureRegion blueBulletTR;
	//-----------------------
	//hud button sprites-----
	Shape hudBackground;
	Sprite menuButton;
	Sprite heartone;
	Sprite hearttwo;
	Sprite heartthree;
	Sprite heartfour;
	Sprite buttonOne;
	//-----------------------
        BulletPool blueBulletPool;
	public MapScreen(platformer game, Camera cam){
		//stuff
		super(2);
		this.game = game;
		viewport = cam;
		removeablePlatforms = new LinkedList<levelPlatform>();
		removeableSprites = new LinkedList<IEntity>();
		loadResources();
		setupDisplay();
	}
	private void loadResources(){
		//load up cstick data
		cBaseT = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		cBaseTR = TextureRegionFactory.createFromAsset(cBaseT, game, "gfx/cBase.png", 0, 0);
		game.getEngine().getTextureManager().loadTexture(cBaseT);
		cKnobT = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		cKnobTR = TextureRegionFactory.createFromAsset(cKnobT, game, "gfx/cKnob.png", 0, 0);
		game.getEngine().getTextureManager().loadTexture(cKnobT);

		//load up hud elements
		hudItemsT = new Texture(128, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		hudMenuBtnTR = TextureRegionFactory.createFromAsset(hudItemsT, game, "gfx/HudMenuButton.png", 0, 0);
		hudHeartEmptyTR = TextureRegionFactory.createFromAsset(hudItemsT, game, "gfx/heartEmpty.png", 0, 17);
		hudHeartEmptyTR = TextureRegionFactory.createFromAsset(hudItemsT, game, "gfx/heartFull.png", 33, 17);

		//load bullet
		bulletsT = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		blueBulletTR = TextureRegionFactory.createFromAsset(bulletsT, game, "gfx/blueBullet.png", 32, 32);
		game.getEngine().getTextureManager().loadTexture(bulletsT);

		blueBulletPool = new BulletPool(this, blueBulletTR, platformer.getPhysicsWorld());
                

		//load up the removal handler
		registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed){
				if(!removeablePlatforms.isEmpty()){
					levelPlatform p = removeablePlatforms.removeFirst();
					detachChild(p.getSpriteRep());
					detachChild(p.getDecor());
					platformer.getPhysicsWorld().unregisterPhysicsConnector(p.getConnector());
					platformer.getPhysicsWorld().destroyBody(p.getBodyRep());
				}
				if(!removeableSprites.isEmpty()){
					detachChild(removeableSprites.removeFirst());
				}
			}
			public void reset(){}
		});
	}
	private void setupDisplay(){
		setBackground(new ColorBackground(255, 0, 0));

		//set up the cStick-----------------
		cStick = new AnalogOnScreenControl(15, 330, viewport, cBaseTR, cKnobTR, 1, new IAnalogOnScreenControlListener() {
			@Override
			public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY){
				if(pValueX != 0){
					Player.getPlayer().traverse(pValueX);
				}
				if(pValueY < 0) {
					Player.getPlayer().jump(pValueY);
				}
			}
			@Override
			public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl){}
		});
		cStick.setScale(cstickScale);
		setChildScene(cStick);
		//==================================
		//second cStick---------------------
		cStickTwo = new AnalogOnScreenControl(585, 330, viewport, cBaseTR, cKnobTR, 1, new IAnalogOnScreenControlListener() {
			@Override
			public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY){
				if(pValueX != 0 || pValueY != 0) {
					blueBulletPool.launch(
                                                Player.getPlayer().getSprite().getX()+33,
                                                Player.getPlayer().getSprite().getY()+33,
                                                Vector2Pool.obtain(2f, -2f)
                                                );
				}
			}
			@Override
			public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl){
			}
		});
		cStickTwo.setScale(cstickScale);
		cStick.setChildScene(cStickTwo);
		//==================================
		registerUpdateHandler(platformer.getPhysicsWorld());
	}
	//screen manager functins---------------
	public void attatch(){
		game.getEngine().setScene(this);
		viewport.setChaseEntity(Player.getPlayer().getSprite());
	}
	public void detatch(){
		viewport.setChaseEntity(null);
		viewport.setCenter(720/2, 480/2);
	}
	//======================================
	//level functions-----------------------
	public void removePlatform(levelPlatform p){
		removeablePlatforms.add(p);
	}
	public void removeEntity(IEntity s){
		removeableSprites.add(s);
	}
	//======================================
}
